package com.iecas.xxx.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.iecas.xxx.common.api.CommonPage;
import com.iecas.xxx.common.api.CommonResult;
import com.iecas.xxx.model.Demo;
import com.iecas.xxx.service.DemoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * Demo表 前端控制器
 * </p>
 *
 * @author iecas
 * @since 2021-09-14
 */
@Api(tags = "Example")
@RestController
@RequestMapping("/example/demo")
public class DemoController {

    private static final Logger LOGGER = LoggerFactory.getLogger(DemoController.class);

    @Autowired
    private DemoService demoService;

    @ApiOperation(value = "获取列表")
    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    public CommonResult<List<Demo>> getDemoList() {
        demoService.cacheDemos();
        return CommonResult.success(demoService.list());
    }

    @ApiOperation(value = "添加")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public CommonResult saveDemo(@Validated @RequestBody Demo demo) {
        CommonResult commonResult;
        int count = 0;
        if (demoService.save(demo)) {
            count = 1;
        }
        if (count == 1) {
            commonResult = CommonResult.success(demo);
            LOGGER.debug("create Demo success:{}", demo);
        } else {
            commonResult = CommonResult.failed("操作失败");
            LOGGER.debug("create Demo failed:{}", demo);
        }
        return commonResult;
    }

    @ApiOperation(value = "更新")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public CommonResult updateDemo(@Validated @RequestBody Demo demo) {
        CommonResult commonResult;
        int count = 0;
        if (demoService.updateById(demo)) {
            count = 1;
        }
        if (count == 1) {
            commonResult = CommonResult.success(demo);
            LOGGER.debug("update Demo success:{}", demo);
        } else {
            commonResult = CommonResult.failed("操作失败");
            LOGGER.debug("update Demo failed:{}", demo);
        }
        return commonResult;
    }

    @ApiOperation(value = "删除")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public CommonResult deleteDemo(@PathVariable("id") Long id) {
        int count = 0;
        if (demoService.removeById(id)) {
            count = 1;
        }
        if (count == 1) {
            LOGGER.debug("delete Demo success :id={}", id);
            return CommonResult.success(null);
        } else {
            LOGGER.debug("delete Demo failed :id={}", id);
            return CommonResult.failed("操作失败");
        }
    }

    @ApiOperation(value = "分页获取列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<Demo>> listDemo(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                   @RequestParam(value = "pageSize", defaultValue = "2") Integer pageSize) {
        IPage<Demo> demoPage = demoService.getDemosByPage(pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(demoPage));
    }

    @ApiOperation(value = "根据编号查询信息")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public CommonResult<Demo> getDemoById(@PathVariable("id") Long id) {
        return CommonResult.success(demoService.getById(id));
    }
}

